package asia.ztclab.voc.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import jxl.common.Logger;

public class FileController {

	private File file = null;
	private Logger logger = null;

	public void directoryCheck(String filePath, String fileName) {
		try {
			file = new File(filePath, fileName);
			if (!file.exists())
				file.mkdirs();
		} catch (Exception e) {
			logger.debug(e.fillInStackTrace());
		}
	}

	public boolean insertContentFile(String contents, String filePath, String fileName) {

		FileWriter fileWriter = null;
		BufferedWriter bufferedWriter = null;

		try {
			fileWriter = new FileWriter(new File(filePath, fileName), true);
			bufferedWriter = new BufferedWriter(fileWriter);

			bufferedWriter.write(contents);
			bufferedWriter.newLine();
		} catch (Exception e) {
			e.getStackTrace();
		} finally {
			try {
				bufferedWriter.close();
				fileWriter.close();
			} catch (Exception e) {
				logger.debug(e.fillInStackTrace());
			}
		}
		return true;
	}

	public ArrayList<String> getfileData(String dirName, String fileName,Boolean isReadLine) {
		BufferedReader bufferedReader = null;
		String str = null;
		String cStr = null;
		List<String> strReturnData = new ArrayList<String>();
		
		try {
			bufferedReader = new BufferedReader(new FileReader(new File(
					dirName, fileName)));

			while (isReadLine == true) {
				str = bufferedReader.readLine();
				strReturnData.add(str);
				if (str == null)
					break;
				// System.out.println(str);
			}

			if (isReadLine == false) {
				while (true) {
					str = bufferedReader.readLine();
					if (str == null)
						break;
					cStr = str;
				}
				strReturnData.add(cStr);
			}
			bufferedReader.close();
		} catch (Exception e) {

		}
		return (ArrayList<String>) strReturnData;
	}

}
